package com.shangxian.giveithome.service.impl;

import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.entity.PageableDetails;
import com.shangxian.giveithome.entity.petCard.PetCardQrCode;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.PetCardQrCodeMapper;
import com.shangxian.giveithome.service.PetCardQrService;
import com.shangxian.giveithome.utils.PageUtils;
import com.shangxian.giveithome.utils.PetCardCodeUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author coderFu
 * @create 2021/5/4
 */
@Service
public class PetCardQrServiceImpl implements PetCardQrService {

    @Resource
    private PetCardQrCodeMapper petCardQrCodeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void produceOneQr() {
        Integer number = petCardQrCodeMapper.findMaxPetCardNo();
        if (number == null){
            number =0;
        }
        try {
            PetCardQrCode petCardQrCode = PetCardCodeUtils.produceOneQr(number+1);
            int count = petCardQrCodeMapper.saveQrCode(petCardQrCode);
            if (count < 0) {
                ExceptionUtils.error(CommonCode.DATA_ERRORS);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void produceHundredQr() {
        Integer number = petCardQrCodeMapper.findMaxPetCardNo();
        if(number == null){
            number = 0;
        }
        int max = number+100;
        try {
            for (; number<=max; number++ ){
                PetCardQrCode petCardQrCode = PetCardCodeUtils.produceOneQr(number+1);
                int count = petCardQrCodeMapper.saveQrCode(petCardQrCode);
                System.out.println(petCardQrCode);
                if (count < 0) {
                    ExceptionUtils.error(CommonCode.DATA_ERRORS);
                }
            }
//            ArrayList<PetCardQrCode> petCardQrCodes = PetCardCodeUtils.produceHundredsQr(number + 1);
//            System.out.println(petCardQrCodes);
//            for (PetCardQrCode petCardQrCode : petCardQrCodes) {
//                int count = petCardQrCodeMapper.saveQrCode(petCardQrCode);
//                if (count < 0) {
//                    ExceptionUtils.error(CommonCode.DATA_ERRORS);
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public PageableDetails findAllPetCardQr(Pageable pageable, String petCardNo) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        if (petCardNo == null || "".equals(petCardNo)){
            int total = petCardQrCodeMapper.findAllPetCardQrTotal(petCardNo);
            List<PetCardQrCode> petCardQrCodes = petCardQrCodeMapper.findAllPetCardQr(pageNumber*pageSize,pageSize);
            return  new  PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),petCardQrCodes);
        }else {
            int total = 1;
            List<PetCardQrCode> petCardQrCodes = petCardQrCodeMapper.findAllPetCardQrByPetNo(petCardNo);
            return new PageableDetails(1,1,1,1,petCardQrCodes);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePetCardQrById(Long id) {
       int count =  petCardQrCodeMapper.deletePetCardQrById(id);
       if (count<1){
           ExceptionUtils.error(CommonCode.DATA_ERRORS);
       }
    }


    /**
     * 返回
     *
     * @param pageNumber 当前页
     * @param pageSize   每页条数
     * @param count      总条数
     * @param list       返回数据
     * @param <T>        泛型
     * @return
     */
    public <T> PageableDetails returnPageable(int pageNumber, Integer pageSize, Integer count, List<T> list) {
        return new PageableDetails<>(pageNumber, pageSize, count, list);
    }


    /**
     * 返回总页数
     *
     * @param total
     * @param pageSize
     * @return
     */
    public int returnPages(int total, int pageSize) {
        return PageUtils.returnPages(total, pageSize);
    }

}
